﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.BAL;
using Zhongchouwwwww.Models;


namespace Zhongchouwwwww.webform
{
    public partial class Admin : System.Web.UI.Page
    {
        private static List<ProjectinfoDTO> projectinfos = new List<ProjectinfoDTO>();
        private static List<Userinfo> userinfos = new List<Userinfo>();
        private static ZhongchouDBEntities db = BAL.GetDate.db;
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Request.QueryString["username"];
            string pwd = Request.QueryString["pwd"];
            if (name == null || pwd == null)
            {
                Response.Redirect("Admin-Login.aspx");
            }
            else
            {
                DataSet();
                Userr.Visible = true;
                Proje.Visible = false;
                Add.Visible = false;
                Launch.Visible = false;
                Returnset.Visible = false;
                Comment.Visible = false;
            }
        }


        /// <summary>
        /// 数据显示
        /// </summary>
        private void DataSet()
        {
            userinfos.Clear();
            projectinfos.Clear();
            userinfos = BAL.GetDate.GetDataSource(new Userinfo());
            foreach (var item in BAL.GetDate.GetDataSource(new Projectinfo()))
            {
                projectinfos.Add(GetProjectDate(item));
            }
            UserData.DataSource = userinfos;
            UserData.DataBind();
            ProjectDate.DataSource = projectinfos;
            ProjectDate.DataBind();
        }


        /// <summary>
        /// 根据项目表的用户id返回用户名
        /// </summary>
        /// <param name="item">项目表数据</param>
        /// <returns></returns>
        private static string GetProjectNameByid(Projectinfo item)
        {
            foreach (var iiii in userinfos)
            {
                if (iiii.userid == item.userid)
                {
                    return iiii.usernikename;
                }
            }
            return "";
        }
        /// <summary>
        /// 数据转换
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static ProjectinfoDTO GetProjectDate(Projectinfo item)
        {
            return new ProjectinfoDTO()
            {
                projectid = item.projectid,
                projectname = item.projectname,
                projectdesc = item.projectdesc,
                targetmoney = item.targetmoney,
                incomemoney = item.incomemoney,
                time = item.time,
                state = item.state,
                ProjectUser = GetProjectNameByid(item)
            };
        }


        protected void UserData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                string id = e.CommandArgument.ToString().Substring(0, 1);
                //string state = e.CommandArgument.ToString().Substring(1, (e.CommandArgument.ToString().Length) - 1);
                string sql = "";
                switch (e.CommandName)
                {
                    case "DDDDD":
                        sql = $"update Userinfo set userstate = '封禁' where userid='{id}'";
                        Update(sql);
                        DataSet();
                        break;
                    case "EEEEE":
                        sql = $"update Userinfo set userstate = '正常' where userid='{id}'";
                        Update(sql);
                        DataSet();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('修改出错！')</script>");
                Response.Redirect("Admin.aspx");
                throw;
            }

        }

        protected void ProjectDate_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                string sql = "";
                string id = e.CommandArgument.ToString();
                switch (e.CommandName)
                {
                    case "AAAA":
                        sql = $"update Projectinfo set state = '众筹中' where projectid = '{id}'";
                        Update(sql);
                        DataSet();
                        break;
                    case "BBBB":
                        sql = $"update Projectinfo set state = '已完成' where projectid = '{id}'";
                        Update(sql);
                        DataSet();
                        break;
                    case "CCCC":
                        sql = $"delete from Projectinfo where projectid = '{id}'";
                        Update(sql);
                        DataSet();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                Response.Redirect("Admin.aspx");
                Response.Write("<script>alert('修改出错！')</script>");
            }
        }

        private void Update(string sql)
        {
            if (BAL.GetDate.EXEXE(sql))
            {
                Response.Write("<script>alert('状态修改成功！')</script>");

            }
            else
            {
                Response.Write("<script>alert('状态修改失败！')</script>");
            }
        }

        protected void userbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = true;
            Proje.Visible = false;
            Add.Visible = false;
            Launch.Visible = false;
            Returnset.Visible = false;
            Comment.Visible = false;
        }

        protected void projectbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = false;
            Proje.Visible = true;
            Add.Visible = false;
            Launch.Visible = false;
            Returnset.Visible = false;
            Comment.Visible = false;
        }

        protected void addressbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = false;
            Proje.Visible = false;
            Add.Visible = true;
            Launch.Visible = false;
            Returnset.Visible = false;
            Comment.Visible = false;
        }

        protected void launchbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = false;
            Proje.Visible = false;
            Add.Visible = false;
            Launch.Visible = true;
            Returnset.Visible = false;
            Comment.Visible = false;
        }

        protected void returnsetbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = false;
            Proje.Visible = false;
            Add.Visible = false;
            Launch.Visible = false;
            Returnset.Visible = true;
            Comment.Visible = false;
        }

        protected void Commentbtn_Click(object sender, EventArgs e)
        {
            Userr.Visible = false;
            Proje.Visible = false;
            Add.Visible = false;
            Launch.Visible = false;
            Returnset.Visible = false;
            Comment.Visible = true;
            CommentDate.DataSource = BAL.GetDate.GetCommentInfoDates("select * from CommentInfo");
            CommentDate.DataBind();

        }

        protected void CommentDate_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                string sql = "";
                string id = e.CommandArgument.ToString();
                if (e.CommandName == "POPOP")
                {
                    sql = $"delete CommentInfo where commentid = '{id}'";
                    if (BAL.GetDate.EXEXE(sql))
                    {
                        Response.Write("<script>alert('删评成功！')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('删评失败！')</script>");
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('删评失败！')</script>");
            }
            
        }
    }
}